package 链表;

import common.ListNode;

/**
 * @author tmh
 * @date 2024/8/21 20:09
 * @description
 */
public class T19删除链表的倒数第N个结点 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        //求出链表的长度
        ListNode p = head;
        int len = 0;
        while (p != null) {
            len++;
            p = p.next;
        }
        p = dummy;
        for (int i = 0; i < len - n; i++) {
            p=p.next;
        }
        //删除节点
        p.next=p.next.next;
        return dummy.next;
    }
}
